<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'header.php'?>

	<div class="page-header">
        <h1><?= isset($warnInfo) ? '修改预警字段' : '添加预警字段' ?></h1>
	</div>
	<div class="col-xs-12">
		<div class="form-horizontal" id="warn-form">
			<input class="hidden" type="text" name="id" value="<?= isset($warnInfo) ? $warnInfo['id'] : '' ?>" />
			<div class="form-group">
				<label class="col-sm-3 control-label no-padding-right">预警字段：</label>
				<div class="col-sm-6">
					<select class="chosen-select form-control tag-input-style" id="key-select" data-placeholder="选择预警字段" 
					name="warn-key" <?= isset($warnInfo) ? 'disabled' : '' ?>>
						<?php foreach($healthWarnKeyData as $key => $value): ?>
							<option value="<?= $key ?>" <?= isset($warnInfo) && $warnInfo['key'] == $key ? 'selected'  : '' ?>><?= $value ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			</div>
			<div class="form-value form-group">
				<label class="col-sm-3 control-label no-padding-right">预警值：</label>
				<div class="div-value col-sm-6">
					<select class="chosen-select form-control tag-input-style" id="value-select" data-placeholder="选择预警值" name="warn-value" multiple="multiple">
						<?php foreach($healthWarnData[$defaultKey] as $key => $value): ?>
							<option value="<?= $key ?>" <?= isset($warnInfo) && in_array($key, $warnInfo['valueArr']) ? 'selected'  : '' ?>><?= $value ?></option>
						<?php endforeach; ?>
					</select>
				</div>
			</div>
			<div class="clearfix">
				<div class="col-md-offset-3 col-md-9">
					<button class="btn btn-info save">
						<i class="ace-icon fa fa-check"></i> 提交
					</button>
				</div>
			</div>
		</div>
	</div>
<?php require VIEWPATH. ADMIN_DIR. DIRECTORY_SEPARATOR. 'footer.php'?>
<script type="text/javascript">
	$(function(){
		var warnArr = JSON.parse('<?= $healthWarnJson ?>');
		// console.log(warnArr);
		// 值下拉选择
		$("#value-select").chosen({
			search_contains: true
		}); 

		// 监听字段下拉框值变化
		var originalValue = "<?= isset($warn['key']) ? $warn['key'] : '' ?>";
		$('#key-select').change(function(){
			// 判断选中值是否变化
			var val = $("#key-select").val();
			if(val === originalValue) {
				// 如果选中值无变化，执行结束
				return;
			}
			// 新值赋值
			originalValue = val;
			// 刷新预警值
			$(".form-value").hide();
			var id = (new Date()).getTime();
			var list = warnArr[val];
			var str = '';
			for(let i in list) {
				str += '<option value="' + i + '">' + list[i] + '</option>';
			}
			$(".clearfix").before('\
			<div class="form-value form-group">\
				<label class="col-sm-3 control-label no-padding-right">预警值：</label>\
				<div class="div-value col-sm-6">\
					<select class="chosen-select form-control tag-input-style" class="value-select" id="' + id + '" data-placeholder="选择预警值"\
					name="warn-value" multiple="multiple">' + str + '\
					</select>\
				</div>\
			</div>\
			');
			$("#" + id).chosen({
				search_contains: true
			});
		});
		// 提交表单
		$(".save").click(function() {
			// 去掉disabled
			$("[name=warn-key]").removeAttr('disabled');
			var id = <?= isset($warnInfo) ? $warnInfo['id'] : 0 ?>;
			var key = $("[name=warn-key]").val();
			var value = $("[name=warn-value]:last").val();
			// var value = $("[name=warn-value]").val();
			// console.log(key);return;
			var url = "<?=admin_url('school/save_health_warn_action')?>";
			var data = {
				id: id,
				key: key,
				value: value
			};
			ajax_post(url, data, function(res){
				if(res.success) {
					window.location.href = "<?=admin_url('school/health_warn')?>";
				}
			});
		})
	})
</script>